<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="../common/tags.jsp" %>   
<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>编辑用户</title>
  </head>
  <script src="<%=path %>/js/jquery.textareaCounter.plugin.js" type="text/javascript"></script>
      <script type="text/javascript">
    
    changWordsLimit("${msgTemplateVO.scope}");
    changeTemplateType("${msgTemplateVO.type}");
    
    function changWordsLimit(value){
    	var targetType = $("#targetType").val();
    	var options = {};
		if(value == 'MSG' || value == 'ALL' || (targetType != null && targetType != '' && targetType != 'COMMON')){
			options = {  
			    'maxCharacterSize': 120,  
			    'originalStyle': 'originalDisplayInfo',  
			    'warningStyle': 'warningDisplayInfo',  
			    'warningNumber': 10,  
			    'displayFormat': '字数#input | 剩余#left'  
			};  
		}else{
			options = {  
		    	'displayFormat': '字数#input'  
			};  
		}
		if($('.charleft')){
				$('.charleft').remove();
		}
		$('#context').unbind( 'keyup' );
		$('#context').unbind( 'mouseover' );
		$('#context').unbind( 'paste' );
		$('#context').textareaCount(options); 
    }
    
    function setTextarea(){
    	var template = $("#template").find("option:selected").text(); 
    	var orinal = $('#context').val();
    	var newValue = "";
    	if($("#template").val() != ""){
    		newValue = orinal + '{' + template + '}';
    		$('#context').val(newValue);
    	}
    }
    	
    function changeTemplateType(value){
    	$("#customerDiv").hide();
    	if(value == "SPECIFIC"){
    		$("#customerDiv").show();
    	}
    	if(value == "ALL"){
    		$("#customerIds").val("${msgTemplateVO.customerIds}");
    	}
    }	
    
        function validate(obj){
    	var targetType  = $("#targetType").val();
    	var content  = $('#context').val();
    	if(content == null){
    		alertMsg.error('模板内容不能为空');
			return false;    	
    	}
    	if(targetType == 'NORECHARGE' && content.indexOf("{天数}") == -1){
			alertMsg.error('无充值提醒模板请插入天数');
			return false;    		
    	}
    	if(targetType == 'NOCOST' && content.indexOf("{天数}") == -1){
			alertMsg.error('无消费提醒模板请插入天数');
			return false;     		
    	}
    	if(targetType == 'LESSREMAIN' && content.indexOf("{金额}") == -1){
			alertMsg.error('余额不足提醒模板请插入金额');
			return false;     		
    	}
    	return validateCallback(obj, navTabAjaxDone);
    }
    
    function changeTarget(value){
    	var scope  = $("#templateScope").val();
    	changWordsLimit(scope);
    	$("#template option").each(function(){
    	   if(this.value != "")
		  	 $(this).hide();
		});
		if(value == "COMMON"){
			showValiables(["NAME","SEX","COMPANY","SCORE","MONEY"]);
		}
		if(value == "BIRTHDAY"){
			showValiables(["NAME","SEX"]);
		}
		if(value == "NORECHARGE"){
			showValiables(["NAME","SEX","DAY"]);
		}
		if(value == "NOCOST"){
			showValiables(["NAME","SEX","DAY"]);
		}
		if(value == "LESSREMAIN"){
			showValiables(["NAME","SEX","COMPANY","MONEY"]);
		}
		if(value == "REMAINCOUNT"){
			showValiables(["NAME","SEX","COMPANY","SCORE","MONEY","COUNT"]);
		}
		if(value == "REGISTER"){
			showValiables(["NAME","SEX","CUSTOMER","TIME"]);
		}
		if(value == "COST"){
			showValiables(["NAME","SEX","TIME","CUSTOMER","COSTMONEY","PRODUCT"]);
		}
		if(value == "COSTCOUNT"){
			showValiables(["NAME","SEX","TIME","COSTCOUNT","CUSTOMER","PRODUCT"]);
		}
		if(value == "SCORE"){
			showValiables(["NAME","SEX","COMPANY","SCORE","ADDSCORE","TIME"]);
		}
		if(value == "COSTSCORE"){
			showValiables(["NAME","SEX","COMPANY","SCORE","COSTSCORE","TIME"]);
		}
		if(value == "RECHARGE"){
			showValiables(["NAME","SEX","MONEY","ADDMONEY","TIME"]);
		}
		if(value == "ADDCOUNT"){
			showValiables(["NAME","SEX","PRODUCT","ADDCOUNT","COUNT","TIME"]);
		}
    }
    
    function showValiables(templates){
    	$.each(templates, function(key,t){
		 	$("#template_"+t).show();
		});
    }
    
	</script>
  
  <body>
	<div class="pageContent">
		<form method="post" action="<%=path %>/msgTemplate/saveMsgTemplate.action" class="pageForm required-validate" onsubmit="return validate(this);">
			<div class="pageFormContent" layoutH="56">
				<input name="msgTemplateVO.id" type="hidden" value="<s:property value="msgTemplateVO.id" />" />
				<p>
					<label>模板标题：</label>
					<input name="msgTemplateVO.name" value="<s:property value="msgTemplateVO.name" />" class="required" type="text" size="30" alt="请输入模板标题"/>
				</p>
				<p>
					<label>模板类型：</label>
					<select name="msgTemplateVO.type"  onchange="changeTemplateType(this.value);"  class="required">
                        <option value="">选择模板类型</option>
                        <c:forEach var="item" items="${types}">
                        	<c:if test="${SESSION_USER.customer==null || (SESSION_USER.customer != null && item.name != 'ALL') }">
                        		<option value="${item.name}" ${msgTemplateVO.type eq item.name?"selected":"" }>${item.title}</option>
                        	</c:if>
                        </c:forEach>
                   </select>
				</p>
				
				<p>
					<label>模板用途：</label>
					<select  id="targetType"  name="msgTemplateVO.targetType"  class="required"  onchange="changeTarget(this.value);">
                        <option value="">选择模板用途</option>
                        <c:forEach var="item" items="${templateTargets}">
                        	<option value="${item.name}" ${msgTemplateVO.targetType eq item.name?"selected":"" }>${item.title}</option>
                        </c:forEach>
                   </select>
				</p>
				
				<shiro:hasPermission name="AddPlateformTypeMsg">
					<p>
						<label>状态：</label>
						<select name="msgTemplateVO.type"  class="required">
	                        <option value="">选择状态</option>
	                        <c:forEach var="item" items="${statuses}">
	                        	<option value="${item.name}" ${msgTemplateVO.type eq item.name?"selected":"" }>${item.title}</option>
	                        </c:forEach>
	                   </select>
					</p>
				</shiro:hasPermission>
				<p  id="customerDiv" style="display:${msgTemplateVO.customerIds == null ?'none':'block'}">
					<label>商户：</label>
					<input id="customerIds" name="msgTemplateVO.customerIds" value="${msgTemplateVO.customerIds}" type="hidden">
					<input name="msgTemplateVO.customerNames" type="text" size="30" value="${msgTemplateVO.customerNames}"/>
					<c:if test="${SESSION_USER.customer==null}">
						<a class="btnLook" href="<%=path %>/customer/listCustomer.action?searchType=msgTemplate&templateId=${msgTemplateVO.id}" lookupGroup="msgTemplateVO">选择</a>
					</c:if>
				</p>
				<p>
					<label>适用范围：</label>
					<select name="msgTemplateVO.scope"  id="templateScope" class="required" onchange="changWordsLimit(this.value);">
                        <option value="">选择适用范围</option>
                        <c:forEach var="item" items="${scopes}">
                        	<option value="${item.name}" ${msgTemplateVO.scope eq item.name?"selected":"" }>${item.title}</option>
                        </c:forEach>
                   </select>
				</p>
				<p>
					<label>插入：</label>
					<select name="msgTemplateVO.template" id="template" onChange="setTextarea();">
						<option value="">==插入==</option>
                        <c:forEach var="item" items="${templates}">
                        	<option value="${item.name}">${item.title}</option>
                        </c:forEach>
                   </select>
				</p>
				<p>
					<label>模板内容：</label>
					<textarea id="context" style="margin-left:60px;margin-top:-15px;" name="msgTemplateVO.content" rows="10" cols="80" class="required">${msgTemplateVO.content}</textarea>
				</p>
			</div>
			<div class="formBar">
				<ul>
					<li><div class="buttonActive"><div class="buttonContent"><button type="submit">保存</button></div></div></li>
					<li>
						<div class="button"><div class="buttonContent"><button type="button" class="close">取消</button></div></div>
					</li>
				</ul>
			</div>
		</form>
	</div>
  </body>
</html>